const defaultIdleTime = 30;//单位minute
/**
 * constructor
 * @param {DOM} container
 * @param {number} idleTime 间隔时间，单位分钟
 * @param {function} inActiveHanle 失效回调函数
 * @param {function} monitoringFn 监控函数，入参为[间隔时长],单位分钟 
 */
class Monitor {
    constructor(container, idleTime, inActiveHanle, monitoringFn) {
        this.startTime = Date.now();
        this.idleTime = idleTime || defaultIdleTime;
        this.container = container;
        this.mouseMoveHanle = this.mouseMoveHanle.bind(this);
        this.register = this.register.bind(this);
        this.destory = this.destory.bind(this);
        this.register();
        this.interval = setInterval(() => {
            let current = Date.now();
            let timeGap = Math.trunc((current - this.startTime) / 60000);//转化为分钟            
            if (typeof monitoringFn === 'function') monitoringFn(timeGap);            
            if (timeGap >= this.idleTime) {
                if (typeof inActiveHanle === 'function') {
                    inActiveHanle();
                }
                this.destory();
            }
        }, 1000);
    }
    mouseMoveHanle() {
        this.startTime = Date.now();
    }
    register() {
        this.container.addEventListener('mousemove', this.mouseMoveHanle)
    }
    destory() {
        this.container.removeEventListener("mousemove", this.mouseMoveHanle);
        clearInterval(this.interval);
    }
}
export default Monitor;